/**
 * 
 */
package com.m7012e.a_remote.ar_listeners;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.leoj.utils.tcp_network.tcp_server.MultiThreadTcpServer;
import com.m7012e.a_remote.ar_listeners.network.protocol.DaemonProtocolFactory;
import com.m7012e.a_remote.ar_listeners.network.protocol.MulticastProtocol;
import com.m7012e.a_remote.commons.ARemoteMulticastListener;
import com.m7012e.a_remote.communication.positioning.Position;
import com.m7012e.a_remote.communication.positioning.SimplePosition;
import com.m7012e.a_remote.communication.positioning.WifiRoughlyPosition;

/**
 * @author leoj
 *
 */
public class ARemoteDaemonLauncher {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		Position daemonPosition = acquirePosition();
		System.out.println("creating a new daemon located at: "+daemonPosition);
		
		//First step build every needed component ... 
		
		//Create a ProtocolFactory
		DaemonProtocolFactory factory = new DaemonProtocolFactory();
		
		//Create a TCP multiThread Server to handle requestMessage
		MultiThreadTcpServer daemonServer = new MultiThreadTcpServer(8707, factory);
		System.out.println("Tcp server address -> "+daemonServer.getServerStrAddress()+":"+daemonServer.getServerPort());
		//Create a MulticastProtocol
		MulticastProtocol multicastProtocol = new MulticastProtocol(factory, daemonPosition, daemonServer.getServerStrAddress(), daemonServer.getServerPort());
		//Create a UDP multicast listener to handle discoveryMessage
		ARemoteMulticastListener multicastListener = new ARemoteMulticastListener(multicastProtocol);
		
		
		//then start everything
		daemonServer.start();
		multicastListener.start();
		
	}
	
	private static Position acquirePosition() throws IOException{
		boolean choiceDone = false;
		Position p = null;
		while(!choiceDone){
			System.out.println("choose between simplePosition (type down 1 and press enter) or WifiRoughlyPosition (type down 2 and press enter");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if(line.equals("1")){
				System.out.println("enter fisrt a value for X then for Y");
				int x = readNumber(reader);
				int y = readNumber(reader);
				p = new SimplePosition(x, y);
				choiceDone = true;
			}else if(line.equals("2")){
				System.out.println("enter first the name of a router then the signal strength");
				System.out.println("enter an empty line to stop");
				p = new WifiRoughlyPosition();
				setupWifiPosition((WifiRoughlyPosition)p, reader);
				choiceDone = true;
			}
		}
		return p;
	}

	/**
	 * @param p
	 * @param reader
	 * @throws IOException 
	 */
	private static void setupWifiPosition(WifiRoughlyPosition p,
			BufferedReader reader) throws IOException {
		String line;
		while(!(line = reader.readLine()).equals("")){
			String name = line;
			int value = readNumber(reader);
			p.addRouter(name, value);
		}
	}

	/**
	 * @return
	 * @throws IOException 
	 */
	private static int readNumber(BufferedReader reader) throws IOException {
		Integer number = null;
		while(number == null){
			String xAxis = reader.readLine();
			try{
				number = Integer.parseInt(xAxis);
			}catch(NumberFormatException e){
				System.out.println("enter a number please ...");
				number = null;
			}
		}
		return 0;
	}
}
